package com.freewater.parking.infrastructure.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class InitTotalParkingRunner implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(InitTotalParkingRunner.class);

    private final SummaryDao summaryDao;

    public InitTotalParkingRunner(SummaryDao summaryDao) {
        this.summaryDao = summaryDao;
    }

    @Override
    public void run(String... args) throws Exception {
        Optional<SummaryTable> summaryTableOpt = this.summaryDao.findById(SummaryTable.SINGLE_ID);
        if (summaryTableOpt.isPresent()) {
            return;
        }
        SummaryTable summaryTable = new SummaryTable();
        summaryTable.setId(SummaryTable.SINGLE_ID);
        summaryTable.setTotalInParking(0);
        this.summaryDao.save(summaryTable);
        log.info("totalParking count initialized to 0");
    }
}
